<template>
  <a-modal
    :title="title"
    :visible="openDialog"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="confirmBtn('commonForm')"
    @cancel="mainDialogClose('commonForm')"
  >
    <a-form-model
      ref="commonForm"
      :model="formData"
      :rules="rules"
      v-bind="layout"
    >
      <a-form-model-item label="模板名称" prop="title">
        <a-input
          ref="input"
          v-model="formData.title"
          @pressEnter="confirmBtn('commonForm')"
        />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import api from "@/server/mouldConfig.js";
import { getRoleId } from "@/unit/tool";

export default {
  name: "addTemplateDialog",
  props: {
    openDialog: Boolean,
    title: String,
    info: Object,
    newMouldType: String
  },
  components: {},
  data() {
    return {
      username: "",
      role: "",
      compId: "",
      compType: "",
      lists: [],
      formData: {
        title: ""
      },
      rules: {
        title: {
          required: true,
          min: 1,
          max: 70,
          message: "请输入1-70位之间的字符",
          trigger: "blur"
        }
      },
      layout: {
        layout: "horizontal",
        labelAlign: "right",
        hideRequiredMark: true,
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      }
    };
  },
  watch: {
    openDialog: {
      immediate: false,
      handler: async function(newVal, oldVal) {}
    }
  },
  methods: {
    mainDialogClose(formName) {
      this.$refs[formName].resetFields();
      this.$emit("update:openDialog", false);
    },
    confirmBtn(formName) {
      let _this = this;
      _this.$refs[formName].validate(async valid => {
        if (!valid) return;
        let { title } = _this.formData;
        // _this.$emit('confirmRefreshData', { title });
        if (_this.newMouldType == "copy" && this.info.id) {
          let data = { name: title, companyId: _this.compId, id: this.info.id };
          api.copyMould(data).then(res => {
            if (res.code != 200) {
              this.$message.error("获取数据失败");
              return;
            }
            _this.$router.push({
              name: "assembleTemplateNew",
              query: { mouldName: title, mouldId: res.data }
            });
            _this.$refs[formName].resetFields();
            _this.$emit("update:openDialog", false);
          });
        }else if(_this.newMouldType == "new") {
        let data = { mouldName: title, companyId: _this.compId };
          const res = await api.addMould(data);
        if (res.code != 200) {
          this.$message.error("获取数据失败");
          return;
        }

        // let { mouldId } = res.data;
        // _this.$router.push({ name: 'assembleTemplateNew', query: { mouldName: title, mouldId: 3 } });
        _this.$router.push({
          name: "assembleTemplateNew",
          query: { mouldName: title, mouldId: res.data }
        });
        _this.$refs[formName].resetFields();
        _this.$emit("update:openDialog", false);
        }
        
      });
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.input.focus();
    });
  },
  created() {
    getRoleId(this);
  }
};
</script>

<style scoped></style>
